package com.sy.olddriver.service.impl;

import com.alibaba.fastjson.JSON;
import com.sy.olddriver.constant.AppConstant;
import com.sy.olddriver.dao.CategoryDao;
import com.sy.olddriver.dao.impl.CategoryDaoImpl;
import com.sy.olddriver.dto.CategoryNode;
import com.sy.olddriver.dto.Result;
import com.sy.olddriver.entity.Category;
import com.sy.olddriver.service.CategoryService;
import com.sy.olddriver.utils.RedisUtils;
import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * CategoryService接口的实现类
 *
 * @author Administrator
 */
public class CategoryServiceImpl implements CategoryService {

    private CategoryDao categoryDao = new CategoryDaoImpl();

    @Override
    public Result getAllCategories() {
        Result result = new Result();
        try {
            //利用Redis作为缓存来优化查询
            List<CategoryNode> categoryNodes;
            //先从Redis中去查询有无对应的分类信息数据
            String categoryNodesJsonStr = RedisUtils.get(AppConstant.CATEGORY_REDIS_KEY);
            //(1)如果Redis中有分类信息数据，则直接返回
            if (StringUtils.isNotBlank(categoryNodesJsonStr)) {
                System.out.println("从缓存中获取分类信息...");
                categoryNodes = JSON.parseArray(categoryNodesJsonStr, CategoryNode.class);
            }
            //(2)如果Redis中没有分类信息数据
            else {
                System.out.println("从数据库获取分类信息...");
                //a.到关系型数据库中查询分类信息
                //查询所有的一级分类
                List<CategoryNode> firstLevelCategoryNodes = new ArrayList<>();
                List<Category> firstLevelCategories = categoryDao.getCategoriesByParentId(AppConstant.FIRST_LEVEL_CATEGORY_PARENT_ID);
                for (Category firstLevelCategory : firstLevelCategories) {
                    CategoryNode firstLevelCategoryNode = new CategoryNode();
                    //设置一级分类本身
                    firstLevelCategoryNode.setSelf(firstLevelCategory);

                    //查询当前一级分类下的所有二级分类
                    List<Category> secondLevelCategories = categoryDao.getCategoriesByParentId(firstLevelCategory.getId());
                    List<CategoryNode> secondLevelCategoryNodes = new ArrayList<>();
                    for (Category secondLevelCategory : secondLevelCategories) {
                        CategoryNode secondLevelCategoryNode = new CategoryNode();
                        secondLevelCategoryNode.setSelf(secondLevelCategory);

                        //设置三级分类
                        List<Category> thirdLevelCategories = categoryDao.getCategoriesByParentId(secondLevelCategory.getId());
                        List<CategoryNode> thirdCategoryNodes = new ArrayList<>();
                        for (Category thirdLevelCategory : thirdLevelCategories) {
                            CategoryNode thirdCategoryNode = new CategoryNode();
                            thirdCategoryNode.setSelf(thirdLevelCategory);
                            thirdCategoryNodes.add(thirdCategoryNode);
                        }
                        secondLevelCategoryNode.setChildren(thirdCategoryNodes);
                        secondLevelCategoryNodes.add(secondLevelCategoryNode);
                    }
                    //设置一级分类对应得二级分类
                    firstLevelCategoryNode.setChildren(secondLevelCategoryNodes);

                    //把当前的一级分类添加到firstLevelCategoryNodes
                    firstLevelCategoryNodes.add(firstLevelCategoryNode);

                }
                //b.将查询到分类信息存入缓存
                categoryNodes = firstLevelCategoryNodes;
                RedisUtils.set(AppConstant.CATEGORY_REDIS_KEY, JSON.toJSONString(categoryNodes));
            }
            result.setData(categoryNodes);
        } catch (Exception e) {
            e.printStackTrace();
            result.setSuccess(false);
            result.setErrMsg(e.getMessage());
        }
        return result;
    }


}
